package com.ruoyi.im.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Tio;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.common.WsSessionContext;
import org.tio.websocket.server.WsServerAioListener;

/**
 * 用户根据情况来完成该类的实现
 */
public class ImServerAioListener extends WsServerAioListener {
    private static Logger log = LoggerFactory.getLogger(ImServerAioListener.class);
    public static final ImServerAioListener me = new ImServerAioListener();

    private ImServerAioListener() {
    }

    /**
     * 连接成功后
     *
     * @param channelContext
     * @param isConnected
     * @param isReconnect
     * @throws Exception
     */
    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
        super.onAfterConnected(channelContext, isConnected, isReconnect);
//        if (log.isInfoEnabled()) {
//            log.info("onAfterConnected\r\n{}", channelContext);
//        }
    }

    /**
     * 发送信息后
     *
     * @param channelContext
     * @param packet
     * @param isSentSuccess
     * @throws Exception
     */
    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
        super.onAfterSent(channelContext, packet, isSentSuccess);
//        if (log.isInfoEnabled()) {
//            log.info("onAfterSent\r\n{}\r\n{}", packet.logstr(), channelContext);
//        }
    }

    /**
     * 在关闭之前
     *
     * @param channelContext
     * @param throwable
     * @param remark
     * @param isRemove
     * @throws Exception
     */
    @Override
    public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception {
        super.onBeforeClose(channelContext, throwable, remark, isRemove);
//        if (log.isInfoEnabled()) {
//            log.info("onBeforeClose\r\n{}", channelContext);
//        }
        WsSessionContext wsSessionContext = (WsSessionContext) channelContext.get();
        if (wsSessionContext != null && wsSessionContext.isHandshaked()) {
            int count = Tio.getAll(channelContext.tioConfig).getObj().size();
            String msg = channelContext.getClientNode().toString() + " 离开了，现在共有【" + count + "】人在线";
            //用tio-websocket，服务器发送到客户端的Packet都是WsResponse
            WsResponse wsResponse = WsResponse.fromText(msg, ImServerConfig.CHARSET);
            //群发
            Tio.sendToGroup(channelContext.tioConfig, Const.GROUP_ID, wsResponse);
        }
    }

    /**
     * 在解码后
     *
     * @param channelContext
     * @param packet
     * @param packetSize
     * @throws Exception
     */
    @Override
    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
        super.onAfterDecoded(channelContext, packet, packetSize);
//        if (log.isInfoEnabled()) {
//            log.info("onAfterDecoded\r\n{}\r\n{}", packet.logstr(), channelContext);
//        }
    }

    /**
     * 接收字节后
     *
     * @param channelContext
     * @param receivedBytes
     * @throws Exception
     */
    @Override
    public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
        super.onAfterReceivedBytes(channelContext, receivedBytes);
//        if (log.isInfoEnabled()) {
//            log.info("onAfterReceivedBytes\r\n{}", channelContext);
//        }
    }

    /**
     * 在处理后
     *
     * @param channelContext
     * @param packet
     * @param cost
     * @throws Exception
     */
    @Override
    public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
        super.onAfterHandled(channelContext, packet, cost);
//        if (log.isInfoEnabled()) {
//            log.info("onAfterHandled\r\n{}\r\n{}", packet.logstr(), channelContext);
//        }
    }
}